package com.zlt.controller;

import com.google.code.kaptcha.Producer;
import com.zlt.pojo.User;
import com.zlt.pojo.PageBean;
import com.zlt.service.IUserService;
import com.zlt.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@Api
public class UserController {
    @Autowired
    IUserService userService;

    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ApiOperation("登录验证的方法")
    public String login(HttpServletResponse response,String validateCode, String username, String password,@RequestHeader("userKey") String userKey){
        System.out.println(response+"/"+validateCode+"/"+username+"/"+ password+"/"+userKey);
        String result = userService.login(response,validateCode, username, password,userKey);
        System.out.println("使用了login方法");
        return result;
    }

    public String register(String fronPassword){
        return "";
    }

    @Autowired
    private Producer captchaProducer;
    @Autowired
    private RedisUtil redisUtil;

    @RequestMapping(value="validateCode",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("生成验证码")
    public Map<String,Object> validateCode(HttpServletResponse response) throws IOException {

        //1. 生成验证码
        String capText = captchaProducer.createText();

        //2. 存储验证码：redis中会存很多验证码  key:value   用户key：用户验证码
        String userKey = UUID.randomUUID().toString();//高并发情况下不安全
        System.out.println("验证码生成的userKey:"+userKey);
        redisUtil.set(userKey,capText); //key-验证码存入redis
        response.setHeader("userKey",userKey);
        System.out.println("查看是否存入"+response.getHeader("userKey"));


        //3. 把验证码图片返回，用户key
        byte[] captcha = null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        BufferedImage bi = captchaProducer.createImage(capText);
        ImageIO.write(bi, "jpg", out);

        captcha = out.toByteArray();
        Map<String, Object> map = new HashMap<>();
        Base64.Encoder encoder = Base64.getEncoder();
        map.put("captcha", "data:image/jpg;base64," + encoder.encodeToString(captcha));
        System.out.println("请求验证码成功");
        return map;
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ApiOperation("add")
    public String add(User user){
        userService.add(user);
        System.out.println("使用了controller add方法");
        return "ok";
    }

    @RequestMapping(value = "del", method = RequestMethod.POST)
    @ApiOperation("del")
    public String del(int id){
        userService.del(id);
        System.out.println("使用了controller del方法");
        return "ok";
    }

    @RequestMapping(value = "edit", method = RequestMethod.POST)
    @ApiOperation("edit")
    public String edit(User user){
        userService.edit(user);
        System.out.println("使用了controller edit方法");
        return "ok";
    }

    @RequestMapping(value = "select", method = RequestMethod.POST)
    @ApiOperation("select")
    public User select(int id){

        System.out.println("使用了controller select方法");
        return userService.select(id);
    }

    /**
     * 分页功能
     * @param   pageNum
     * @return  PageBean<User>
     */
    @RequestMapping(value = "pageBreak", method = RequestMethod.POST)
    @ApiOperation("select")
    public PageBean<User> pageBreak(String pageNum){
        System.out.println("使用了controller pageBreak方法");
        PageBean<User> datas = userService.pageBreak(pageNum);
        return datas;
    }





}
